package com.jiangjiang.eshop.membership.updater;

import com.jiangjiang.eshop.membership.constant.MemberLevel;
import com.jiangjiang.eshop.membership.domain.MemberLevelDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberLevelEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointEntity;
import com.jiangjiang.eshop.membership.service.MemberLevelDetailService;
import com.jiangjiang.eshop.membership.service.MemberLevelService;
import com.jiangjiang.eshop.membership.service.MemberPointDetailService;
import com.jiangjiang.eshop.membership.service.MemberPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName PayOrderMembershipUpdater
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/6 23:18
 * @Version 1.0
 **/
@Component
public class PayOrderMembershipUpdater extends AbstractMembershipUpdater<Object,Boolean> {
    /**
     * 支付订单的会员成长值累加值比例
     */
    private static final Double PAY_ORDER_GROWTH_VALUE_UPDATE_RATE = 0.1;
    /**
     * 支付订单的会员积分累加值比例
     */
    private static final Double PAY_ORDER_MEMBER_POINT_UPDATE_RATE = 0.1;

    /**
     * 会员等级管理Service组件
     */
    @Autowired
    private MemberLevelService memberLevelService;
    /**
     * 会员积分管理Service组件
     */
    @Autowired
    private MemberPointService memberPointService;


    @Autowired
    public PayOrderMembershipUpdater(MemberLevelDetailService memberLevelDetailService,
                                     MemberPointDetailService memberPointDetailService) {
        super(memberLevelDetailService, memberPointDetailService);
    }

    @Override
    protected MemberLevelDetailEntity updateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        Long totalAmount = (Long) parameter;
        MemberLevelDetailEntity detail = new MemberLevelDetailEntity();
        MemberLevelEntity entity = memberLevelService.queryByAccountId(userAccountId);
        // 记录旧值
        detail.setOldMemberLevel(entity.getLevel());
        detail.setOldGrowthValue(entity.getGrowthValue());
        // 执行更新计划
        entity.setGrowthValue(entity.getGrowthValue() + Math.round(totalAmount * PAY_ORDER_GROWTH_VALUE_UPDATE_RATE));
        entity.setLevel(MemberLevel.get(entity.getGrowthValue()));
        memberLevelService.updateById(entity);
        // 记录更新后的新值
        detail.setNewMemberLevel(entity.getLevel());
        detail.setNewGrowthValue(entity.getGrowthValue());
        detail.setUpdatedGrowthValue(Math.round(totalAmount * PAY_ORDER_GROWTH_VALUE_UPDATE_RATE));
        return detail;
    }

    @Override
    protected MemberPointDetailEntity updateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        Long totalAmount = (Long) parameter;
        MemberPointDetailEntity detail = new MemberPointDetailEntity();
        MemberPointEntity entity = memberPointService.queryByAccountId(userAccountId);
        // 记录旧值
        detail.setOldMemberPoint(entity.getPoint());
        // 执行更新计划
        entity.setPoint(entity.getPoint() + Math.round(totalAmount * PAY_ORDER_MEMBER_POINT_UPDATE_RATE));
        memberPointService.updateById(entity);
        // 记录更新后的数据
        detail.setNewMemberPoint(entity.getPoint());
        detail.setUpdatedMemberPoint(Math.round(totalAmount * PAY_ORDER_MEMBER_POINT_UPDATE_RATE));
        return detail;
    }

    @Override
    protected Boolean getExecuteResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }

    @Override
    protected String getMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return  "支付了订单，订单金额为：" + parameter;
    }

    @Override
    protected String getMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return "支付了订单，订单金额为：" + parameter;
    }

    @Override
    protected MemberLevelDetailEntity undoUpdateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        Long totalAmount = (Long) parameter;
        MemberLevelDetailEntity detail = new MemberLevelDetailEntity();
        MemberLevelEntity entity = memberLevelService.queryByAccountId(userAccountId);
        // 记录旧值
        detail.setOldMemberLevel(entity.getLevel());
        detail.setOldGrowthValue(entity.getGrowthValue());
        // 执行更新计划
        entity.setGrowthValue(entity.getGrowthValue() - Math.round(totalAmount * PAY_ORDER_GROWTH_VALUE_UPDATE_RATE));
        entity.setLevel(MemberLevel.get(entity.getGrowthValue()));
        memberLevelService.updateById(entity);

        detail.setNewMemberLevel(entity.getLevel());
        detail.setNewGrowthValue(entity.getGrowthValue());
        detail.setUpdatedGrowthValue(- Math.round(totalAmount * PAY_ORDER_GROWTH_VALUE_UPDATE_RATE));
        return detail;
    }

    @Override
    protected MemberPointDetailEntity undoUpdateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        Long totalAmount = (Long) parameter;
        MemberPointDetailEntity detail = new MemberPointDetailEntity();
        MemberPointEntity entity = memberPointService.queryByAccountId(userAccountId);
        // 记录旧值
        detail.setOldMemberPoint(entity.getPoint());
        // 执行更新计划
        entity.setPoint(entity.getPoint() - Math.round(totalAmount * PAY_ORDER_MEMBER_POINT_UPDATE_RATE));
        memberPointService.updateById(entity);
        // 记录更新后的数据
        detail.setNewMemberPoint(entity.getPoint());
        detail.setUpdatedMemberPoint(- Math.round(totalAmount * PAY_ORDER_MEMBER_POINT_UPDATE_RATE));
        return detail;
    }

    @Override
    protected Boolean getUndoResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }

    @Override
    protected String getUndoMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return "对订单进行了退货，订单金额为：" + parameter;
    }

    @Override
    protected String getUndoMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return "对订单进行了退货，订单金额为：" + parameter;
    }
}
